<template>
  <div>
    <a-card :bordered="false" :bodyStyle="tstyle">
      <div class="table-page-search-wrapper" v-if="hasPerm('questionnaire:page')">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="标题">
                <a-input v-model="queryParam.title" allow-clear placeholder="请输入标题"/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="问卷类型">
                <a-select style="width: 100%" v-model="queryParam.type" placeholder="请选择问卷类型">
                  <a-select-option v-for="(item,index) in typeData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="是否甄别问卷">
                <a-select style="width: 100%" v-model="queryParam.isScreening" placeholder="请选择是否甄别问卷">
                  <a-select-option v-for="(item,index) in whetherData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="截止时间">
                <a-date-picker style="width: 100%" placeholder="请选择截止时间" v-model="queryParam.endTimeDate" @change="onChangeendTime"/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24" >
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)" >查询</a-button>
                <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false">
      <s-table
        ref="table"
        :columns="columns"
        :data="loadData"
        :alert="options.alert"
        :rowKey="(record) => record.id"
        :rowSelection="options.rowSelection"
      >
        <template class="table-operator" slot="operator" v-if="hasPerm('questionnaire:add')" >
          <a-button type="primary" v-if="hasPerm('questionnaire:add')" icon="plus" @click="$refs.addForm.add()">新增问卷表</a-button>
          <a-button type="danger" :disabled="selectedRowKeys.length < 1" v-if="hasPerm('questionnaire:delete')" @click="batchDelete"><a-icon type="delete"/>批量删除</a-button>
          <x-down
            v-if="hasPerm('questionnaire:export')"
            ref="batchExport"
            @batchExport="batchExport"
          />
        </template>
        <span slot="questionnaireCategoryIdScopedSlots" slot-scope="text">
          {{ myDataType(text,typeData) }}
        </span>
        <span slot="isScreeningScopedSlots" slot-scope="text">
          {{ 'whether' | dictType(text) }}
        </span>
        <span slot="action" slot-scope="text, record">
          <a v-if="hasPerm('questionnaire:edit')" @click="$refs.editForm.edit(record)">编辑</a>
          <a-divider type="vertical" v-if="hasPerm('questionnaire:edit') || hasPerm('questionnaire:delete') || hasPerm('questionnaireSubject:page')"/>
          <a-dropdown v-if="hasPerm('questionnaire:delete')||hasPerm('questionnaireSubject:page')">
            <a class="ant-dropdown-link">
              更多 <a-icon type="down" />
            </a>
            <a-menu slot="overlay">
              <a-menu-item v-if="hasPerm('questionnaireSubject:page')">
                <a @click="$refs.questionnairesubjectIndex.init(record.id)">问卷题目管理</a>
              </a-menu-item>
              <a-menu-item v-if="hasPerm('questionnaire:delete')">
                <a-popconfirm placement="topRight" title="确认删除？" @confirm="() => singleDelete(record)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </s-table>
      <add-form ref="addForm" @ok="handleOk" />
      <edit-form ref="editForm" @ok="handleOk" />
      <questionnairesubject-index ref="questionnairesubjectIndex" />
    </a-card>
  </div>
</template>
<script>
  import { STable, XDown } from '@/components'
  import moment from 'moment'
  import { questionnairePage, questionnaireDelete, questionnaireExport } from '@/api/modular/main/questionnaire/questionnaireManage'
  import addForm from './addForm.vue'
  import editForm from './editForm.vue'
  import questionnairesubjectIndex from '../questionnairesubject/index'
  import { questionnaireCategoryList } from '@/api/modular/main/questionnairecategory/questionnaireCategoryManage'
  import Vue from 'vue'
  export default {
    components: {
      STable,
      addForm,
      editForm,
      XDown,
      questionnairesubjectIndex
    },
    data () {
      return {
        // 高级搜索 展开/关闭
        advanced: false,
        // 查询参数
        queryParam: {},
        // 表头
        columns: [
          {
            title: '标题',
            align: 'center',
            dataIndex: 'title'
          },
          {
            title: '问卷类型',
            align: 'center',
            dataIndex: 'questionnaireCategoryId',
            scopedSlots: { customRender: 'questionnaireCategoryIdScopedSlots' }
          },
          {
            title: '是否甄别问卷',
            align: 'center',
            dataIndex: 'isScreening',
            scopedSlots: { customRender: 'isScreeningScopedSlots' }
          },
          {
            title: '价格(单位元)',
            align: 'center',
            dataIndex: 'price'
          },
          {
            title: '截止时间',
            align: 'center',
            dataIndex: 'endTime'
          }
        ],
        tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return questionnairePage(Object.assign(parameter, this.switchingDate())).then((res) => {
            return res.data
          })
        },
        typeData: [],
        whetherData: [],
        selectedRowKeys: [],
        selectedRows: [],
        options: {
          alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
          rowSelection: {
            selectedRowKeys: this.selectedRowKeys,
            onChange: this.onSelectChange
          }
        }
      }
    },
    created () {
      if (this.hasPerm('questionnaire:edit') || this.hasPerm('questionnaire:delete')) {
        this.columns.push({
          title: '操作',
          width: '150px',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
        })
      }
      questionnaireCategoryList().then(res => {
        this.typeData = res.data
      })
      this.whetherData = this.$options.filters['dictData']('whether')
    },
    methods: {
      moment,
      /**
       * 查询参数组装
       */
      switchingDate () {
        const queryParamendTime = this.queryParam.endTimeDate
        if (queryParamendTime != null) {
            this.queryParam.endTime = moment(queryParamendTime).format('YYYY-MM-DD')
            if (queryParamendTime.length < 1) {
                delete this.queryParam.endTime
            }
        }
        const obj = JSON.parse(JSON.stringify(this.queryParam))
        return obj
      },
      /**
       * 单个删除
       */
      singleDelete (record) {
        const param = [{ 'id': record.id }]
        this.questionnaireDelete(param)
      },
      /**
       * 批量删除
       */
      batchDelete () {
        const paramIds = this.selectedRowKeys.map((d) => {
            return { 'id': d }
        })
        this.questionnaireDelete(paramIds)
      },
      questionnaireDelete (record) {
        questionnaireDelete(record).then((res) => {
          if (res.success) {
            this.$message.success('删除成功')
            this.$refs.table.clearRefreshSelected()
          } else {
            this.$message.error('删除失败') // + res.message
          }
        })
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      onChangeendTime(date, dateString) {
        this.endTimeDateString = dateString
      },
      /**
       * 批量导出
       */
      batchExport () {
        const paramIds = this.selectedRowKeys.map((d) => {
            return { 'id': d }
        })
        questionnaireExport(paramIds).then((res) => {
            this.$refs.batchExport.downloadfile(res)
        })
      },
      handleOk () {
        this.$refs.table.refresh()
      },
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      },
      myDataType(id,data){
        console.log(data)
        for(var i = 0 ; i < data.length; i++){
          if(data[i].id === id){
            return data[i].name;
          }
        }
      }
    }
  }
</script>
<style lang="less">
  .table-operator {
    margin-bottom: 18px;
  }
  button {
    margin-right: 8px;
  }
</style>
